﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;

namespace SysObj
{
    public class L_HangHoa : Lib.Bases.BaseClass
    {

        #region Prop
        public System.Int32 IdHangHoa
        {
            get;
            set;
        }

        public System.String MaHang
        {
            get;
            set;
        }

        public System.String TenHang
        {
            get;
            set;
        }

        public System.Int32 IdDVT
        {
            get;
            set;
        }

        public System.Int32 IdNhom
        {
            get;
            set;
        }

        public System.Int32 IdLoai
        {
            get;
            set;
        }

        public System.String GhiChu
        {
            get;
            set;
        }

        public byte[] HinhAnh { get; set; }
        public bool ThucDon { get; set; }
        public bool NoStock { get; set; }
        #endregion

        #region  Constructors
        public L_HangHoa()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }

        public L_HangHoa(System.Int32 v_IdHangHoa)
        {
            IdHangHoa = v_IdHangHoa;
            ReadData();
        }

        #endregion

        #region Methods
        public override void ResetData()
        {
            IdHangHoa = 0;
            MaHang = string.Empty;
            TenHang = string.Empty;
            IdDVT = 0;
            IdNhom = 0;
            IdLoai = 0;
            HinhAnh = null;
            GhiChu = string.Empty;
            ThucDon = false;
            NoStock = false;
        }

        public void ReadData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = m_DataLayer.ExecDataTable("SP_L_HangHoa", "Command,IdHangHoa", "LoadData", IdHangHoa);
                this.ResetData();
                if (dt == null || dt.Rows.Count <= 0)
                    return ;
                this.FillData(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override DataTable LoadData()
        {
            return m_DataLayer.ExecDataTable("SP_L_HangHoa", "Command", "GetList");
        }

        public DataTable GetAll()
        {
            return m_DataLayer.ExecDataTable("SP_L_HangHoa", "Command", "GetAll");
        }

        public DataTable GetMenuItemByGroupId(int v_IdNhom)
        {
            return m_DataLayer.ExecDataTable("SP_L_HangHoa", "Command,IdNhom", "GetMenuItemByGroupId", v_IdNhom);
        }

        public DataTable ListMenu()
        {
            return m_DataLayer.ExecDataTable("SP_L_HangHoa", "Command", "ListMenu");
        }

        public DataTable GetMenuWithGroup()
        {
            return m_DataLayer.ExecDataTable("SP_L_HangHoa", "Command", "GetMenuWithGroup");
        }

        public override void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdHangHoa"))
                IdHangHoa = Lib.Data.ValueType.GetValue(row["IdHangHoa"], IdHangHoa);
            if (row.Table.Columns.Contains("MaHang"))
                MaHang = Lib.Data.ValueType.GetValue(row["MaHang"], MaHang);
            if (row.Table.Columns.Contains("TenHang"))
                TenHang = Lib.Data.ValueType.GetValue(row["TenHang"], TenHang);
            if (row.Table.Columns.Contains("IdDVT"))
                IdDVT = Lib.Data.ValueType.GetValue(row["IdDVT"], IdDVT);
            if (row.Table.Columns.Contains("IdNhom"))
                IdNhom = Lib.Data.ValueType.GetValue(row["IdNhom"], IdNhom);
            if (row.Table.Columns.Contains("IdLoai"))
                IdLoai = Lib.Data.ValueType.GetValue(row["IdLoai"], IdLoai);
            if (row.Table.Columns.Contains("GhiChu"))
                GhiChu = Lib.Data.ValueType.GetValue(row["GhiChu"], GhiChu);
            if (row.Table.Columns.Contains("HinhAnh"))
                HinhAnh = Lib.Data.ValueType.GetValue(row["HinhAnh"], HinhAnh);
            if (row.Table.Columns.Contains("ThucDon"))
                ThucDon = Lib.Data.ValueType.GetValue(row["ThucDon"], ThucDon);
            if (row.Table.Columns.Contains("NoStock"))
                NoStock = Lib.Data.ValueType.GetValue(row["NoStock"], NoStock);
        }

        public override void OnUpdate()
        {
            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Update");
                Lib.Data.ParamHolder.AddParam("IdHangHoa", IdHangHoa);
                Lib.Data.ParamHolder.AddParam("MaHang", MaHang);
                Lib.Data.ParamHolder.AddParam("TenHang", TenHang);
                Lib.Data.ParamHolder.AddParam("IdDVT", IdDVT);
                Lib.Data.ParamHolder.AddParam("IdNhom", IdNhom);
                Lib.Data.ParamHolder.AddParam("IdLoai", IdLoai);
                Lib.Data.ParamHolder.AddParam("GhiChu", GhiChu);
                Lib.Data.ParamHolder.AddParam("HinhAnh", HinhAnh);
                Lib.Data.ParamHolder.AddParam("ThucDon", ThucDon);
                Lib.Data.ParamHolder.AddParam("NoStock", NoStock);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_L_HANGHOA", s_ParamName, ref output, o_ParamValue);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override void OnDelete()
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Delete");
                Lib.Data.ParamHolder.AddParam("IdHangHoa", IdHangHoa);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_L_HANGHOA", s_ParamName, o_ParamValue);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
